package com.kkb.retailer.center.controller;


import com.github.pagehelper.PageInfo;
import com.kkb.retailer.center.api.GoodsInfoService;
import com.kkb.retailer.center.common.http.HttpResult;
import com.kkb.retailer.center.common.pojo.*;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("api")
public class GoodsInfoController {

    @Reference
    private GoodsInfoService goodsInfoService;





    @PostMapping(value = "/queryGoodsList")
    public HttpResult queryGoodsList(@RequestBody GoodsQueryCondition queryCondition){
        PageInfo<GoodsInfo> goods = goodsInfoService.queryAllByCondition(queryCondition);
        return HttpResult.success(goods);
    }


    @ResponseBody
    @PostMapping("/addProduct")
    public HttpResult addTbProduct(@RequestBody ProductInfoVo tbProductVo){
        ProductInfo productInfo = (ProductInfo)tbProductVo;
        int result=goodsInfoService.addProductInfo(productInfo,tbProductVo.getExtrAttribute(), tbProductVo.getIntroduce());
        return HttpResult.success(result);
    }







    @ResponseBody
    @PostMapping("/relatedImg")
    public HttpResult relatedImg(@RequestBody Map<String,Object> map){
        Long goodsId = Long.valueOf((Integer) map.get("goodsId"));
        List<String> imgs = (List<String>) map.get("img");
        //先删除当前id下的所有图片
        goodsInfoService.deleteImgByGoodsId(goodsId);
        GoodsAttach goodsAttach = new GoodsAttach();
        goodsAttach.setGoodsId(goodsId);
        int result = 0;
        for (String img : imgs) {
            goodsAttach.setPath(img);
            goodsInfoService.relatedImg(goodsAttach);
            result++;
        }
        return HttpResult.success(result);
    }



    @ResponseBody
    @GetMapping("/getImg")
    public HttpResult getImg(Long goodsId){
        List<GoodsAttach> list = goodsInfoService.getImgs(goodsId);
        List<String> arrayList = new ArrayList<>();
        for (GoodsAttach goodsAttach : list) {
            arrayList.add(goodsAttach.getPath());
        }
        return HttpResult.success(arrayList);
    }

    @PutMapping(value = "/StoreProduct")
    public HttpResult update(@RequestBody GoodsInfo info){
        int result = goodsInfoService.updateGoods(info);
        goodsInfoService.staticPageBuild(info);
        return HttpResult.success(result);
    }
}
